
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>1.2 Spring集成smart-socket · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../SUMMARY.html" />
    
    
    <link rel="prev" href="../2.1-基础应用/" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../">
            
                <a href="../../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" >
            
                <span>
            
                    
                    第一章 smart-socket开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../2.1-基础应用/">
            
                <a href="../2.1-基础应用/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2.2" data-path="./">
            
                <a href="./">
            
                    
                    1.2 Spring集成smart-socket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../SUMMARY.html">
            
                <a href="../SUMMARY.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../chapter-2/">
            
                <a href="../../chapter-2/">
            
                    
                    第二章 源码解析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="../../chapter-2/核心接口/">
            
                <a href="../../chapter-2/核心接口/">
            
                    
                    2.1 核心接口与状态机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="../../chapter-2/服务端/">
            
                <a href="../../chapter-2/服务端/">
            
                    
                    2.2 服务端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="../../chapter-2/客户端/">
            
                <a href="../../chapter-2/客户端/">
            
                    
                    2.3 客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="../../chapter-2/AioSession/">
            
                <a href="../../chapter-2/AioSession/">
            
                    
                    2.4 会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="../../chapter-2/SUMMARY.html">
            
                <a href="../../chapter-2/SUMMARY.html">
            
                    
                    2.5 小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../chapter-3/">
            
                <a href="../../chapter-3/">
            
                    
                    第三章 通信协议
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../../chapter-3/1-消息归类/">
            
                <a href="../../chapter-3/1-消息归类/">
            
                    
                    3.1 消息归类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.2 半包/粘包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.3 Http消息解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../../BLANK.html">
            
                <a href="../../BLANK.html">
            
                    
                    3.4 RPC消息解析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../chapter-5/">
            
                <a href="../../chapter-5/">
            
                    
                    第四章 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../chapter-6/">
            
                <a href="../../chapter-6/">
            
                    
                    第五章 未来规划
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../../end/">
            
                <a href="../../end/">
            
                    
                    附录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-http 实战手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../../smart-http/">
            
                <a href="../../../smart-http/">
            
                    
                    关于smart-http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" >
            
                <span>
            
                    
                    第一章 smart-http开发指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../../../smart-http/chapter-1/">
            
                <a href="../../../smart-http/chapter-1/">
            
                    
                    1.1 快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="../../../smart-http/chapter-1/http_route.html">
            
                <a href="../../../smart-http/chapter-1/http_route.html">
            
                    
                    1.2 请求路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="../../../smart-http/chapter-1/file_upload.html">
            
                <a href="../../../smart-http/chapter-1/file_upload.html">
            
                    
                    1.3 文件上传/下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="../../../smart-http/chapter-1/websocket.html">
            
                <a href="../../../smart-http/chapter-1/websocket.html">
            
                    
                    1.4 WebSocket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../../share/firstfit/readme.html">
            
                <a href="../../../share/firstfit/readme.html">
            
                    
                    内存管理-首次适应算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../../share/thread/readme.html">
            
                <a href="../../../share/thread/readme.html">
            
                    
                    Thread知多少
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../../donation.html">
            
                <a href="../../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >1.2 Spring集成smart-socket</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="spring&#x96C6;&#x6210;smart-socket">Spring&#x96C6;&#x6210;smart-socket</h2>
<p>&#x73B0;&#x4ECA;&#x7684;&#x4F01;&#x4E1A;&#x7CFB;&#x7EDF;&#x5F00;&#x53D1;&#x901A;&#x5E38;&#x4F1A;&#x4F7F;&#x7528;Spring&#xFF0C;smart-socket&#x4E0E;Spring&#x7684;&#x96C6;&#x6210;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x57FA;&#x672C;&#x7684;&#x901A;&#x4FE1;&#x670D;&#x52A1;Bean&#x5B9E;&#x4F8B;&#x6258;&#x7BA1;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>&#x6B64;&#x5904;&#x4EE5;xml&#x914D;&#x7F6E;&#x548C;&#x6CE8;&#x89E3;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x4E3A;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;smart-socket&#x4E8E;spring&#x7684;&#x96C6;&#x6210;&#x65B9;&#x6848;&#xFF0C;&#x524D;&#x671F;&#x9700;&#x8981;&#x505A;&#x7684;&#x51C6;&#x5907;&#x5DE5;&#x4F5C;&#x5C31;&#x662F;&#x5148;&#x642D;&#x5EFA;&#x4E00;&#x4E2A;spring&#x5DE5;&#x7A0B;&#xFF0C;&#x5E76;&#x5F15;&#x5165;smart-socket&#x4F9D;&#x8D56;&#xFF0C;pom.xml&#x914D;&#x7F6E;&#x5982;&#x4E0B;&#x56FE;&#x3002;</p>
<p><img src="spring-example-1.png" width="80%"></p>
<h3 id="1-xml&#x914D;&#x7F6E;&#x5316;&#x542F;&#x52A8;&#x670D;&#x52A1;">1. xml&#x914D;&#x7F6E;&#x5316;&#x542F;&#x52A8;&#x670D;&#x52A1;</h3>
<p>&#x200B;    &#x901A;&#x8FC7;2.1&#x7AE0;&#x8282;&#x6211;&#x4EEC;&#x4E86;&#x89E3;&#x5230;smart-socket&#x542F;&#x7528;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x4F9D;&#x8D56;&#x4E24;&#x4E2A;&#x5173;&#x952E;&#x7684;&#x8981;&#x7D20;&#xFF1A;Protocol&#x3001;MessageProcessor&#xFF0C;&#x5728;spring&#x7684;&#x96C6;&#x6210;&#x5E94;&#x7528;&#x4E2D;&#x6211;&#x4EEC;&#x4F9D;&#x65E7;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x5B83;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x4F1A;&#x4EE5;&#x670D;&#x52A1;&#x7AEF;&#x573A;&#x666F;&#x4E3A;&#x4F8B;&#x7ED9;&#x5927;&#x5BB6;&#x6F14;&#x793A;&#xFF0C;&#x5982;&#x679C;&#x60A8;&#x662F;&#x8981;&#x8FDB;&#x884C;&#x5BA2;&#x6237;&#x7AEF;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x4E0E;Spring&#x7684;&#x96C6;&#x6210;&#xFF0C;&#x8BF7;&#x6309;&#x540C;&#x6837;&#x7684;&#x64CD;&#x4F5C;&#x65B9;&#x5F0F;&#x66FF;&#x6362;&#x4E00;&#x4E0B;&#x76F8;&#x5E94;&#x7684;&#x914D;&#x7F6E;&#x5373;&#x53EF;&#x3002;</p>
<ul>
<li><p>&#x5B9A;&#x4E49;&#x534F;&#x8BAE;&#x7C7B;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringProtocol</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Protocol</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">decode</span><span class="hljs-params">(ByteBuffer readBuffer, AioSession&lt;String&gt; session)</span> </span>{
        <span class="hljs-keyword">int</span> remaining = readBuffer.remaining();
        <span class="hljs-keyword">if</span> (remaining &lt; Integer.BYTES) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        readBuffer.mark();
        <span class="hljs-keyword">int</span> length = readBuffer.getInt();
        <span class="hljs-keyword">if</span> (length &gt; readBuffer.remaining()) {
            readBuffer.reset();
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        <span class="hljs-keyword">byte</span>[] b = <span class="hljs-keyword">new</span> <span class="hljs-keyword">byte</span>[length];
        readBuffer.get(b);
        readBuffer.mark();
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> String(b);
    }
}
</code></pre>
</li>
<li><p>&#x5B9A;&#x4E49;&#x5904;&#x7406;&#x5668;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServerProcessor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageProcessor</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
     <span class="hljs-meta">@Override</span>
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(AioSession&lt;String&gt; session, String msg)</span> </span>{
         WriteBuffer writeBuffer = session.writeBuffer();
         <span class="hljs-keyword">byte</span>[] bytes = msg.getBytes();

         <span class="hljs-keyword">try</span> {
             writeBuffer.writeInt(bytes.length);
             writeBuffer.write(bytes);
         } <span class="hljs-keyword">catch</span> (IOException e) {
             e.printStackTrace();
         }
     }

     <span class="hljs-meta">@Override</span>
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">stateEvent</span><span class="hljs-params">(AioSession&lt;String&gt; session, StateMachineEnum stateMachineEnum, Throwable throwable)</span> </span>{
     }
}
</code></pre>
</li>
</ul>
<p>&#x51C6;&#x5907;&#x5DE5;&#x4F5C;&#x5C31;&#x7EEA;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;application.xml&#x914D;&#x7F6E;&#x5B83;&#x4EEC;&#x7684;&#x5B9E;&#x4F8B;bean&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x5F15;&#x7528;&#x81F3;AioQuickServer&#x7684;bean&#x914D;&#x7F6E;&#x3002;&#x56E0;&#x4E3A;<code>AioQuickServer</code>&#x7684;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x90FD;&#x662F;&#x5E26;&#x53C2;&#x6570;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x914D;&#x7F6E;bean&#x7684;&#x65F6;&#x5019;&#x9700;&#x8981;&#x7528;&#x5230;&#x6807;&#x7B7E;<code>constructor-arg</code>&#x3002;</p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span><span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span>
       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd&quot;</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;protocol&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.smartboot.example.spring.StringProtocol&quot;</span>/&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;messageProcessor&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.smartboot.example.spring.ServerProcessor&quot;</span>/&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;aioQuickServer&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.smartboot.socket.transport.AioQuickServer&quot;</span> <span class="hljs-attr">init-method</span>=<span class="hljs-string">&quot;start&quot;</span> <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">&quot;shutdown&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">&quot;0&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;8080&quot;</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">&quot;1&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;protocol&quot;</span>/&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">&quot;2&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;messageProcessor&quot;</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<p>&#x5F53;&#x542F;&#x52A8;spring&#x5BB9;&#x5668;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x901A;&#x4FE1;&#x670D;&#x52A1;&#x4FBF;&#x5F00;&#x59CB;&#x8FD0;&#x884C;&#x3002;
&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x6765;&#x9A8C;&#x8BC1;&#x4E00;&#x4E0B;&#x96C6;&#x6210;&#x540E;&#x7684;&#x6548;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x5C06;&#x5176;&#x914D;&#x7F6E;&#x5230;&#x771F;&#x6B63;&#x7684;web&#x670D;&#x52A1;&#x4E2D;&#x6F14;&#x793A;&#x8FC7;&#x7A0B;&#x7A0D;&#x663E;&#x7410;&#x788E;&#xFF0C;&#x6545;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x901A;&#x8FC7;main&#x51FD;&#x6570;&#x6765;&#x8C03;&#x7528;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringDemo</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        ApplicationContext context = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">&quot;application.xml&quot;</span>);
        AioQuickServer aioQuickServer = context.getBean(<span class="hljs-string">&quot;aioQuickServer&quot;</span>, AioQuickServer.class);
        System.out.println(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x542F;&#x52A8;&#x6210;&#x529F;&#xFF1A;&quot;</span> + aioQuickServer);
        ((ClassPathXmlApplicationContext) context).close();
        System.out.println(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x5173;&#x95ED;&quot;</span>);
    }
}
</code></pre>
<p>&#x6267;&#x884C;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x540E;&#x63A7;&#x5236;&#x53F0;&#x4F1A;&#x6253;&#x5370;&#x670D;&#x52A1;&#x7684;&#x542F;&#x52A8;&#x4E0E;&#x5173;&#x95ED;&#x65E5;&#x5FD7;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#xFF0C;&#x8BF7;&#x68C0;&#x67E5;&#x7AEF;&#x53E3;&#x53F7;&#x662F;&#x5426;&#x88AB;&#x5360;&#x7528;&#x3002;</p>
<h3 id="2-&#x6CE8;&#x89E3;&#x65B9;&#x5F0F;&#x542F;&#x52A8;&#x670D;&#x52A1;">2. &#x6CE8;&#x89E3;&#x65B9;&#x5F0F;&#x542F;&#x52A8;&#x670D;&#x52A1;</h3>
<p>&#x5982;&#x679C;&#x8BFB;&#x8005;&#x4E60;&#x60EF;&#x7528;&#x6CE8;&#x89E3;&#x7684;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;Spring&#xFF0C;&#x90A3;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x539F;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x7A0D;&#x52A0;&#x6539;&#x52A8;&#x3002;Protocol&#x548C;MessageProcessor&#x5B9E;&#x73B0;&#x7C7B;&#x9700;&#x8981;&#x52A0;&#x4E0A;&#x6CE8;&#x89E3;<code>@Component</code>&#x3002;</p>
<ul>
<li><p>&#x5B9A;&#x4E49;&#x534F;&#x8BAE;&#x7C7B;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>(<span class="hljs-string">&quot;protocol&quot;</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StringProtocol</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Protocol</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">decode</span><span class="hljs-params">(ByteBuffer readBuffer, AioSession&lt;String&gt; session)</span> </span>{
        <span class="hljs-keyword">int</span> remaining = readBuffer.remaining();
        <span class="hljs-keyword">if</span> (remaining &lt; Integer.BYTES) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        readBuffer.mark();
        <span class="hljs-keyword">int</span> length = readBuffer.getInt();
        <span class="hljs-keyword">if</span> (length &gt; readBuffer.remaining()) {
            readBuffer.reset();
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
        <span class="hljs-keyword">byte</span>[] b = <span class="hljs-keyword">new</span> <span class="hljs-keyword">byte</span>[length];
        readBuffer.get(b);
        readBuffer.mark();
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> String(b);
    }
}
</code></pre>
</li>
<li><p>&#x5B9A;&#x4E49;&#x5904;&#x7406;&#x5668;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>(<span class="hljs-string">&quot;messageProcessor&quot;</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServerProcessor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageProcessor</span>&lt;<span class="hljs-title">String</span>&gt; </span>{
      <span class="hljs-meta">@Override</span>
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(AioSession&lt;String&gt; session, String msg)</span> </span>{
          WriteBuffer writeBuffer = session.writeBuffer();
          <span class="hljs-keyword">byte</span>[] bytes = msg.getBytes();

          <span class="hljs-keyword">try</span> {
              writeBuffer.writeInt(bytes.length);
              writeBuffer.write(bytes);
          } <span class="hljs-keyword">catch</span> (IOException e) {
              e.printStackTrace();
          }
      }

      <span class="hljs-meta">@Override</span>
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">stateEvent</span><span class="hljs-params">(AioSession&lt;String&gt; session, StateMachineEnum stateMachineEnum, Throwable throwable)</span> </span>{
      }
  }
</code></pre>
</li>
</ul>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x4FEE;&#x6539;application.xml&#x914D;&#x7F6E;&#xFF0C;<code>default-autowire=&quot;byName&quot;</code>&#x8868;&#x793A;&#x4F18;&#x5148;&#x6309;bean&#x540D;&#x79F0;&#x6CE8;&#x5165;&#xFF0C;&#x800C;&#x6CE8;&#x89E3;&#x7684;&#x626B;&#x63CF;&#x626B;&#x63CF;&#x5305;&#x8DEF;&#x5F84;&#x4E3A;<code>org.smartboot.example.spring</code>&#x3002;</p>
<pre><code class="lang-xml"><span class="php"><span class="hljs-meta">&lt;?</span>xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span><span class="hljs-meta">?&gt;</span></span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span>
       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
       <span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span>
       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd&quot;</span>
       <span class="hljs-attr">default-autowire</span>=<span class="hljs-string">&quot;byName&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;org.smartboot.example.spring&quot;</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<p>&#x6700;&#x540E;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x4EE5;&#x6CE8;&#x89E3;&#x7684;&#x5F62;&#x5F0F;&#x6784;&#x9020;AioQuickServer&#x5BF9;&#x8C61;&#x5E76;&#x542F;&#x52A8;&#x670D;&#x52A1;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SpringDemo</span> </span>{

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> MessageProcessor messageProcessor;

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> Protocol protocol;

    <span class="hljs-keyword">private</span> AioQuickServer aioQuickServer;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        ApplicationContext context = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">&quot;application.xml&quot;</span>);
        SpringDemo demo = context.getBean(<span class="hljs-string">&quot;springDemo&quot;</span>, SpringDemo.class);
        System.out.println(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x542F;&#x52A8;&#x6210;&#x529F;&#xFF1A;&quot;</span> + demo.aioQuickServer);
        ((ClassPathXmlApplicationContext) context).close();
        System.out.println(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x5173;&#x95ED;&quot;</span>);
    }

    <span class="hljs-meta">@PostConstruct</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>{
        aioQuickServer = <span class="hljs-keyword">new</span> AioQuickServer(<span class="hljs-number">8080</span>, protocol, messageProcessor);
        <span class="hljs-keyword">try</span> {
            aioQuickServer.start();
        } <span class="hljs-keyword">catch</span> (IOException e) {
            e.printStackTrace();
        }
    }

    <span class="hljs-meta">@PreDestroy</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>{
        aioQuickServer.shutdown();
    }
}
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-07-07 13:05:12
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../2.1-基础应用/" class="navigation navigation-prev " aria-label="Previous page: 1.1 快速上手">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../SUMMARY.html" class="navigation navigation-next " aria-label="Next page: 小结">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"1.2 Spring集成smart-socket","level":"1.2.2.2","depth":3,"next":{"title":"小结","level":"1.2.2.3","depth":3,"path":"smart-socket/chapter-1/SUMMARY.md","ref":"smart-socket/chapter-1/SUMMARY.md","articles":[]},"previous":{"title":"1.1 快速上手","level":"1.2.2.1","depth":3,"path":"smart-socket/chapter-1/2.1-基础应用/README.md","ref":"smart-socket/chapter-1/2.1-基础应用/README.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/book/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/chapter-1/2.2-Spring集成/README.md","mtime":"2019-07-07T05:05:12.431Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2020-04-02T05:22:17.739Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

